<html lang="zh-CN">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport"
        content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
    <title>稻荷社区入站答题募集</title>
    <link rel="icon" href="../icon.png" sizes="any" />
    <link rel="stylesheet" href="css/style.css" />
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
    <script>
    function showlog() {
        var logstate = document.getElementById('log_content').style.display;
        if (logstate == 'none') {
            document.getElementById('log_content').style.display = 'block';
            document.getElementById('log_title').innerHTML = '更新日志：';
        } else {
            document.getElementById('log_content').style.display = 'none';
            document.getElementById('log_title').innerHTML = '点击查看更新日志';
        }
    }

    function nopic_trigger() {
        document.getElementById('upload_div').style.display = 'none';
        document.getElementById('upload_info').innerHTML = '';
    }

    function withpic_trigger() {
        document.getElementById('upload_div').style.display = 'block';
    }

    function submit_reset() {
        fd = new FormData(document.forms.question_upload);
        fd.set('classify', fd.getAll('classify'));
        $.ajax({
            url: "server.php",
            type: "post",
            data: fd,
            processData: false,
            contentType: false,
            success: function(res) {
                if (res == '1') {
                    document.getElementById('submit_info').innerHTML = "已成功提交到服务器！";
                    reset();
                } else if (res == '0') {
                    document.getElementById('submit_info').innerHTML = "提交失败，请联系团子OvO";
                } else if (res == '999') {
                    document.getElementById('submit_info').innerHTML = "验证码错误QAQ<br>你是不是对网页做了什么手脚？";
                }
            },
            error: function(err) {
                alert("题库后台连接失败", err);
            }
        });
    }

    function reset() {
        //document.getElementById('question_upload').submit();
        document.getElementsByName('classify').forEach((item) => {
            item.checked = false;
        });
        document.getElementsByName('question')[0].value = '';
        document.getElementsByName('correct_ans')[0].value = '';
        document.getElementsByName('ans_2')[0].value = '';
        document.getElementsByName('ans_3')[0].value = '';
        document.getElementsByName('ans_4')[0].value = '';
        document.getElementById('withpic').checked = false;
        document.getElementById('nopic').checked = true;
        document.getElementById('picurl').value = '';
        document.getElementById('upload_info').innerHTML = '';
        document.getElementById('captcha_img').click();
        document.getElementsByName('captcha')[0].value = '';
        document.getElementById('captcha_info').innerHTML = "";
        nopic_trigger();
    }

    function uploadquestion_ajax() {
        var with_pic = false;
        fd = new FormData(document.forms.question_upload);
        if (document.getElementsByName('question')[0].value == '') {
            alert("请完善题干信息！");
            return false;
        } else if (
            document.getElementsByName('correct_ans')[0].value == '' |
            document.getElementsByName('ans_2')[0].value == '' |
            document.getElementsByName('ans_3')[0].value == '' |
            document.getElementsByName('ans_4')[0].value == ''
        ) {
            alert("请完善选项信息！");
            return false;
        } else if (document.getElementsByName('author')[0].value == '') {
            alert("请填写出题人！");
            return false;
        } else if (fd.getAll('classify').length <= 0) {
            alert("请选择分类！");
            return false;
        } else {
            var form = document.getElementById('captcha'),
                formData = new FormData(form);
            $.ajax({
                url: "../captcha/captcha_check.php",
                type: "post",
                data: formData,
                processData: false,
                contentType: false,
                success: function(res) {
                    if (res == '1') {
                        document.getElementById('captcha_info').innerHTML = "验证码正确！";
                        if (document.getElementById('withpic').checked) {
                            if (document.getElementById('picurl').value == '') {
                                uploadPic();
                            }
                        } else if (document.getElementById('nopic').checked) {
                            document.getElementById('picurl').value = '';
                            submit_reset();
                        }
                    } else if (res == '0') {
                        document.getElementById('captcha_info').innerHTML = "验证码错误！";
                    }
                },
                error: function(err) {
                    alert("验证码后台连接失败", err);
                }
            });
        }
    }

    function uploadPic() {
        if (document.getElementById('pic').value == '') {
            alert("请选择要上传的图片！");
            return false;
        }

        var form = document.getElementById('pic_upload'),
            formData = new FormData(form);
        document.getElementById('upload_info').innerHTML = '正在上传...';
        $.ajax({
            url: "https://file.drmt.xyz/images",
            type: "post",
            data: formData,
            processData: false,
            contentType: false,
            success: function(res) {
                if (res) {
                    //alert("上传成功！");
                    domparser = new DOMParser();
                    doc = domparser.parseFromString(res, 'text/html');
                    pic_url = doc.getElementsByClassName('mdui-textfield-input')[0].value;
                    console.log(pic_url);
                }
                $("#pic").val("");
                $(".showUrl").val(pic_url);
                //$(".showPic").attr("src", pic_url);
                submit_reset();
            },
            error: function(err) {
                alert("网络连接失败,稍后重试", err);
            }

        })
    }
    </script>
</head>

<body>
    <div id="main">
        <div id="title">稻荷社区入站答题募集</div>
        <div id="header">
            这里可以为稻荷社区<a href="..">入站题库</a>进行投稿~<br>
            请大家稍微把握一下题目难度哦~不要太难<br>
            所有题目都会经过审核才能收录到题库<br>
            不适合作为入站题的题目会收录到<a href="../hard/">"地狱难度"</a>版本<br>
            当前题库状态：<br>
            <?php
            include "../tools/tools.php";

            $db = local_db();

            $sheet = 'questions';
            $sql = "SELECT * FROM `$sheet`";
            $result = $db->query($sql);
            $num_eazy_all = $result->num_rows;
            $sheet = 'questions_hard';
            $sql = "SELECT * FROM `$sheet`";
            $result = $db->query($sql);
            $num_hard_all = $result->num_rows;
            $num_all = $num_eazy_all+$num_hard_all;
            $sql = "SELECT * FROM `questions_con` WHERE `checked`='0'";
            $result = $db->query($sql);
            $num_unchecked_all = $result->num_rows;

            echo '<span>';
            echo '<span style="color:blue"> '.$num_all.'</span>';
            if ($num_unchecked_all!=0) echo '<span style="color:purple">+'.$num_unchecked_all.'</span>';
            echo '/<span style="color:green">'.$num_eazy_all.'</span>/';
            echo '<span style="color:red">'.$num_hard_all.'</span></span>';
            ?>
            <span style="font-size:medium"><span style="color:blue">题库总数</span><span
                    style="color:purple">(+待审核)</span>/<span style="color:green">简单</span>/<span
                    style="color:red">困难</span></span>
        </div>
        <div id="update_log">
            <span id="log_title" onclick="showlog()">点击查看更新日志<br></span>
            <div id="log_content">2022.10.8:<br>
                修复 验证码il1, oO0长得太像的问题<br>
                修复 验证码有可能出现空白字符的bug<br>
                优化 验证码显示效果<br>
                新增 更新日志显示<br>
                新增 题库及各分类状态显示<br>
                新增 提交前未选择标签会给出提示<br>
                2022.10.7:<br>
                新增 题目标签选择，方便后续开发<br>
                （比如说让用户选择自己擅长的分类什么的<br><br>

                也欢迎大家来反馈bug~<br>
            </div>
        </div>
        <div id="editor">
            <form id="question_upload" method="POST">
                <div class="block">
                    <div class="lable">题目</div>
                    <textarea name="question" class="bigbox textin" form="question_upload" required></textarea>
                </div>
                <div class="block">
                    <div class="lable">正确答案</div>
                    <textarea name="correct_ans" class="smallbox textin" form="question_upload" required></textarea>
                </div>
                <div class="block">
                    <div class="lable">错误答案1</div>
                    <textarea name="ans_2" class="smallbox textin" form="question_upload" required></textarea>
                </div>
                <div class="block">
                    <div class="lable">错误答案2</div>
                    <textarea name="ans_3" class="smallbox textin" form="question_upload" required></textarea>
                </div>
                <div class="block">
                    <div class="lable">错误答案3</div>
                    <textarea name="ans_4" class="smallbox textin" form="question_upload" required></textarea>
                </div>
                <div class="block">
                    <div class="lable">相关标签<span style="font-size:small">可多项选择</span></div>
                    <div class="q_classify">
                        <div class="column1">
                            <?php
                            $class_label = array(
                                'Kanon'=>'Kanon系列','Air'=>'AIR系列','Clannad_TA'=>'Clannad/TA系列',
                                'planetarian'=>'星之梦系列','LittleBusters_KW'=>'Little Busters!/KW系列','AngelBeats'=>'Angel Beats!系列',
                                'Rewrite'=>'Rewrite系列','Charlotte'=>'Charlotte系列','Harmonia'=>'Harmonia系列',
                                'SummerPockets'=>'Summer Pockets系列','Kami_Hi'=>'成神之日系列','Loopers'=>'Loopers系列',
                                'PrimaDoll'=>'Prima Doll系列','Kaginado'=>'键等系列','LUNARiA'=>'LUNARiA系列','HBR'=>'Heaven Burns Read系列',
                                'Stella'=>'星之终途系列','pre_Key'=>'Key前身相关','Staff'=>'CV/Staff相关','Music'=>'KSL/音乐相关','other'=>'其他'
                            );
                            $class_num = count($class_label);
                            $column2_count = (int)($class_num/2);
                            $i = 0;
                            foreach($class_label as $k=>$v){
                                $sheet = 'questions';
                                $sql = "SELECT * FROM `$sheet` INNER JOIN `questions_con` ON `$sheet`.`contribute_No`=`questions_con`.`No` WHERE `questions_con`.`label` LIKE '%$k%'";
                                $result = $db->query($sql);
                                $num_eazy = $result->num_rows;
                                $sheet = 'questions_hard';
                                $sql = "SELECT * FROM `$sheet` INNER JOIN `questions_con` ON `$sheet`.`contribute_No`=`questions_con`.`No` WHERE `questions_con`.`label` LIKE '%$k%'";
                                $result = $db->query($sql);
                                $num_hard = $result->num_rows;
                                $num_total = $num_eazy + $num_hard;
                                $sql = "SELECT * FROM `questions_con` WHERE `label` LIKE '%$k%' AND `checked`='0'";
                                $result = $db->query($sql);
                                $num_unchecked = $result->num_rows;

                                echo '<input id="'.$k.'" type="checkbox" name="classify" value="'.$k.'" form="question_upload">';
                                echo '<label for="'.$k.'"> '.$v;
                                echo '<span style="font-size:10px">';
                                echo '<span style="color:blue"> '.$num_total.'</span>';
                                if ($num_unchecked!=0) echo '<span style="color:purple">+'.$num_unchecked.'</span>';
                                echo '/<span style="color:green">'.$num_eazy.'</span>/';
                                echo '<span style="color:red">'.$num_hard.'</span></span>';
                                echo '</label>';
                                echo '</input><br>';
                                if($i==$column2_count){
                                    echo '</div>';
                                    echo '<div class="column2">';
                                }
                                $i++;
                            }
                            ?>
                        </div>
                    </div>
                </div>
                <div class="block">
                    <div class="lable">出题人</div>
                    <textarea name="author" class="box textin" form="question_upload" required></textarea>
                </div>
                <div class="block">
                    <input id="nopic" type="radio" name="with_pic" value="0" onclick="nopic_trigger()"
                        form="question_upload" checked>
                    <label for="nopic">无图片</label>
                    </input>
                    <input id="withpic" type="radio" name="with_pic" value="1" onclick="withpic_trigger()"
                        form="question_upload">
                    <label for="withpic">有图片</label>
                    </input>
                    <div id="upload_div" style="display:none">
                        <input type="file" name="file" id="pic" form="pic_upload" />
                        <!--<input type="button" value="上传图片" onclick="uploadPic()" form="pic_upload"/>-->
                        <br>
                        <!--<label>图片链接(会自动生成)</lable>-->
                        <input id="picurl" type="text" name="picurl" class="showUrl" form="question_upload"
                            style="display:none"></input>
                        <span id="upload_info"></span>
                        <!--   <img src="" class="showPic" alt=""> -->
                    </div>
                </div>
                <div class="block">
                    <div class="lable">验证码<span style="font-size:small">不分大小写</span></div>
                    <img id="captcha_img" src="../captcha/captcha.php"
                        onclick="this.src='../captcha/captcha.php?'+new Date().getTime();" width="200"><br />
                    <input id="verify" type="text" name="captcha" placeholder="验证码" form="captcha" class="textin">
                    <input itype="text" form="captcha" style="display:none" />
                    <span id="captcha_info"></span>
                </div>
            </form>
            <form id="pic_upload" enctype="multipart/form-data" method="post">
            </form>
            <form id="captcha" action="" method="POST">
            </form>
            <div class="block submit">
                <input class="submit_buttom" type="button" value="提交" onclick="uploadquestion_ajax()"
                    form="question_upload">
                <br>
                <div id="submit_info" class="submit_info"></div>
            </div>


        </div>

    </div>
</body>

</html>